package Parsers;
import java.util.ArrayList;
import java.util.Collection;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import BusinessObjects.Activity;
import Main.MainProgram;
public class ActivityParser extends DefaultHandler{
private MainProgram m;
private Collection<Activity> activities;
private Collection<String> activitiesToDelete;
private Activity currentActivity;
private String baliseMere="";
private String currentBalise="";
public ActivityParser(MainProgram main){
this.m = main;
this.activities = new ArrayList<Activity>();
this.activitiesToDelete = new ArrayList<String>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("activite")){
currentActivity = new Activity();
}
if (localName.equals("ID") && baliseMere.equals("delete"))
this.activitiesToDelete.add(attributes.getValue(0));
if (localName.equals("activite") || localName.equals("ville") || localName.equals("delete") || localName.equals("listeAvis"))
baliseMere=localName;
currentBalise=localName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equals("activite"))
this.activities.add(currentActivity);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String value = new String(ch, start, length);
//On supprime les espaces inutiles
value = value.replaceAll("[\t\n]+", "");
if (value.equals("") || value.equals(" "))
return;
if (baliseMere.equals("activite")){
if (currentBalise.equals("nom"))
currentActivity.setNom(value);
if (currentBalise.equals("date"))
currentActivity.setDate(value);
if (currentBalise.equals("description"))
currentActivity.setDescription(value);
if (currentBalise.equals("urlPhoto"))
currentActivity.setPhoto(value);
}
if (baliseMere.equals("ville")){
if (currentBalise.equals("nom"))
currentActivity.setNomVille(value);
if (currentBalise.equals("region"))
currentActivity.setRegion(value);
if (currentBalise.equals("pays"))
currentActivity.setPays(value);
}
if (currentBalise.equals("type"))
currentActivity.setType(value);
if (currentBalise.equals("avis"))
currentActivity.getAvis().add(value);
}
@Override
public void endDocument() throws SAXException {
//On donne au programme main la liste d'activités à créer
m.setActivitiesToCreate(activities);
m.setActivitiesToDelete(activitiesToDelete);
System.out.println("Fin du fichier : "+MainProgram.activitiesOperations);
}
}